package com.airsystem.pos.bengkel.controller.dialog;

import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.Karyawan;
import com.airsystem.pos.bengkel.interfaces.ILookup;
import com.airsystem.pos.bengkel.interfaces.ISystem;
import com.airsystem.pos.bengkel.model.dialog.MasterEmployeeDialogModel;
import com.airsystem.pos.bengkel.service.KaryawanServices;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class MasterEmployeeDialogController extends MasterEmployeeDialogModel implements ILookup, ISystem {
	private static final Logger LOG = Logger.getLogger(MasterEmployeeDialogController.class.getSimpleName());

	private String search;

	private KaryawanServices karyawanServices = new KaryawanServices();

	public MasterEmployeeDialogController() {
		super();
	}

	public void setup() {
		TableFrame.deleteTable(tableData, tableModel);
	}

	public void get() {
		setNama(tableData.getValueAt(row, 0).toString());
		setGaji(tableData.getValueAt(row, 1).toString().replace(".00", ""));
		setUangMakan(tableData.getValueAt(row, 2).toString().replace(".00", ""));
		setId(tableData.getValueAt(row, 3).toString());
	}

	public void reset() {
		searchField.setText("");
		searchCombo.requestFocus();
	}

	public void category() {
		switch (searchCombo.getSelectedIndex()) {
		case 0:
			search = "nama";
			break;
		}
	}

	public void search() {
		if (searchField.getText().trim().isEmpty()) {
			JOptionPane.showMessageDialog(MasterEmployeeDialogController.this, "Silahkan input data!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();
		} else {
			try {
				category();
				setup();

				List<Karyawan> karyawans = karyawanServices.select(search, searchField.getText());
				for (Karyawan karyawan : karyawans) {
					Vector<String> vector = new Vector<String>();
					vector.add(karyawan.getNama());
					vector.add(karyawan.getGaji() == null ? "0" : String.valueOf(karyawan.getGaji().intValue()));
					vector.add(karyawan.getUang_makan() == null ? "0" : String.valueOf(karyawan.getUang_makan().intValue()));
					vector.add(karyawan.getId().toString());
					tableModel.addRow(vector);
				}

				tableData.setModel(tableModel);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				JOptionPane.showMessageDialog(MasterEmployeeDialogController.this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void load() {
		try {
			List<Karyawan> karyawans = karyawanServices.select();
			for (Karyawan karyawan : karyawans) {
				Vector<String> vector = new Vector<String>();
				vector.add(karyawan.getNama());
				vector.add(karyawan.getGaji() == null ? "0" : String.valueOf(karyawan.getGaji().intValue()));
				vector.add(karyawan.getUang_makan() == null ? "0" : String.valueOf(karyawan.getUang_makan().intValue()));
				vector.add(karyawan.getId().toString());
				tableModel.addRow(vector);
			}
			tableData.setModel(tableModel);

			this.setVisible(true);
		} catch (Exception e) {
			this.setVisible(false);
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(MasterEmployeeDialogController.this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}